package com.haois.mp.database.module.wxserver.rest;

import com.haois.mp.database.commom.enums.ApiResultEnum;
import com.haois.mp.wechat.config.MenuConfig;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import com.haois.mp.database.module.basic.controller.BaseController;

import com.haois.mp.database.module.wxserver.service.WxUserService;
import com.haois.mp.database.module.wxserver.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * Created by RuiHaoZ on 2018/1/27 2:50
 */
@RestController
@RequestMapping("/api/wxmp")
public class WxMpController extends BaseController {

    @Autowired
    private WxUserService sysService;
    @Autowired
    private MenuConfig menuConfig;

    /**
     * @api {POST} /api/wxmp/menu/create 更新微信自定义菜单
     * @apiGroup apiWxMpMenu
     * @apiName createMenu
     * @apiDescription 更新微信自定义菜单
     * @apiParamExample {json} 请求样例：
     * POST /api/wxmp/menu/create
     */
    @PostMapping("/menu/create")
    public Map createMenu() throws WxErrorException, UnsupportedEncodingException {
        WxMenu wxMenu = menuConfig.createWxMenu();
        String result = wxMpService.getMenuService().menuCreate(wxMenu);
        Map map = MapUtil.createMap(ApiResultEnum.API_00.getCode(), "自定义菜单创建成功", result);
        return map;
    }
    /**
     * @api {GET} /api/wxmp/user/list 获取公众号粉丝
     * @apiGroup apiWxMpUser
     * @apiName getUserList
     * @apiDescription  获取公众号粉丝openId LIST
     * @apiParamExample {json} 请求样例：
     * GET /api/wxmp/user/list
     */
    @GetMapping("/user/list")
    public Map getUserList() throws WxErrorException {
        WxMpUserList result = wxMpService.getUserService().userList(null);
        Map map = MapUtil.createMap(ApiResultEnum.API_00.getCode(), "获取用户列表成功", result);
        return map;
    }
    /**
     * @api {PUT} /api/wxmp/user/list 更新数据库粉丝状态
     * @apiGroup apiWxMpUser
     * @apiName putUserList
     * @apiDescription 更新数据库粉丝状态
     * @apiParamExample {json} 请求样例：
     * PUT /api/wxmp/user/list
     */
    @PutMapping("/user/list")
    public Map putUserList() throws WxErrorException {
        List<WxMpUser> wxUsers = sysService.syncWxUserByDB();
        Map map = MapUtil.createMap(ApiResultEnum.API_00.getCode(), "更新用户列表成功", wxUsers);
        return map;
    }
    /**
     * @api {POST} /api/wxmp/user/list 同步公众号粉丝信息
     * @apiGroup apiWxMpUser
     * @apiName postUserList
     * @apiDescription 同步粉丝信息
     * @apiParamExample {json} 请求样例：
     * POST /api/wxmp/user/list
     */
    @PostMapping("/user/list")
    public Map postUserList() throws WxErrorException {
        List<WxMpUser> wxUsers = sysService.syncWxUser();
        Map map = MapUtil.createMap(ApiResultEnum.API_00.getCode(), "更新用户列表成功", wxUsers);
        return map;
    }
    /**
     * @api {GET} /api/wxmp/user/{openId} 获取单个粉丝信息
     * @apiGroup apiWxMpUser
     * @apiName getUser
     * @apiDescription 获取单个粉丝信息
     * @apiParamExample {json} 请求样例：
     * GET /api/wxmp/user/{openId}
     */
    @GetMapping("/user/{openId}")
    public Map getUser(@PathVariable String openId) throws WxErrorException {
        WxMpUser result = wxMpService.getUserService().userInfo(openId);
        Map map = MapUtil.createMap(ApiResultEnum.API_00.getCode(), "获取用户信息成功", result);
        return map;
    }
    /**
     * @api {POST} /api/wxmp/user/{openId} 同步单个粉丝信息
     * @apiGroup apiWxMpUser
     * @apiName postUser
     * @apiDescription 同步粉丝信息
     * @apiParamExample {json} 请求样例：
     * POST /api/wxmp/user/{openId}
     */
    @PostMapping("/user/{openId}")
    public Map postUser(@PathVariable String openId) throws WxErrorException {
        WxMpUser wxMpUser = sysService.syncWxUser(openId);
        Map map = MapUtil.createMap(ApiResultEnum.API_00.getCode(), "同步信息成功", wxMpUser);
        return map;
    }
}
